package com.graduate.cec.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.CarouselMapper;
import com.graduate.cec.pojo.dto.CarouselDTO;
import com.graduate.cec.pojo.dto.CarouselSimpleDTO;
import com.graduate.cec.pojo.dto.UserSimpleDTO;
import com.graduate.cec.pojo.entity.Carousel;
import com.graduate.cec.pojo.entity.CarouselExample;
import com.graduate.cec.pojo.entity.User;
import com.graduate.cec.pojo.entity.UserExample;
import com.graduate.cec.pojo.vo.CarouselSimpleVO;
import com.graduate.cec.pojo.vo.CarouselVO;
import com.graduate.cec.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class CarouselService {
    @Resource
    private CarouselMapper carouselMapper;
    String dirPath = null;

    /****************        增       ******************************************************************/
    public void insertCarousel(CarouselDTO carouselDTO){
        Carousel carousel = CopyUtil.copy(carouselDTO,Carousel.class);
        int i = carouselMapper.insertSelective(carousel);
        if (i == 1){
            return ;
        }else {
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }


    //上传图片
    public String uploadPictures(MultipartFile picFile) throws Exception {
        String urlStr = URLUtil.getURL(FileUtil.file("articlePictures")).toString();
        dirPath = urlStr.substring(urlStr.indexOf("cec"), urlStr.indexOf("target"));
        String prefix = urlStr.substring(6, urlStr.indexOf("cec"));
        dirPath += "articlePictures/";
        dirPath = prefix + dirPath;

        String fileName = picFile.getOriginalFilename();

        //得到后缀名 从最后一个.出现的位置截取到最后
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //得到唯一文件名 UUID.randomUUID()得到一个唯一标识符
        fileName = UUID.randomUUID() + suffix;
        log.info("fileName = " + fileName);
        //准备保存图片的文件夹路径
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        //得到文件的完整路径
        String filePath = dirPath + fileName;
        log.info("filePath = " + filePath);
        //把文件保存到此路径
        picFile.transferTo(new File(filePath));
        return "http://localhost:19090/articlePictures/" + fileName;
    }



    /****************        删       ******************************************************************/
    public void deleteByKeys(Integer id){
        int i = carouselMapper.deleteByPrimaryKey(id);
        if (i != 0){
            return ;
        }else{
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }

    //删除上传的图片
    public void removePicture(String pictureName) {
        String filePath = dirPath + "/" + pictureName;
        new File(filePath).delete();
    }





    /****************        改       ******************************************************************/
    public void updateByKey(CarouselDTO carouselDTO){
        Carousel carousel = CopyUtil.copy(carouselDTO, Carousel.class);
        int i = carouselMapper.updateByPrimaryKeySelective(carousel);
        if (i == 1){
            return;
        }else{
            throw new CecServiceException(ResponseCode.UPDATE_INFO_ERROR,"修改失败");
        }
    }

    /****************        查       ******************************************************************/
    public JsonPage<CarouselVO> getListByExample(CarouselSimpleDTO carouselSimpleDTO){
        PageHelper.startPage(carouselSimpleDTO.getPageNum(), carouselSimpleDTO.getPageSize());
        List<Carousel> carousels = carouselMapper.selectByExampleWithBLOBs(new CarouselExample());
        List<CarouselVO> carouselVOS = CopyUtil.copyList(carousels, CarouselVO.class);
        return JsonPage.restPage(new PageInfo<>(carouselVOS));
    }

    public List<CarouselVO> getAllList(){
        List<Carousel> carousels = carouselMapper.selectByExampleWithBLOBs(new CarouselExample());
        List<CarouselVO> carouselVOS = CopyUtil.copyList(carousels, CarouselVO.class);
        return carouselVOS;

    }


}
